<?php
class NewsModel extends Model {
	protected $_validate = array(
	    array('title','require','请填写标题！'),
		
    );
	protected $_auto = array(
        //array('datetime','time',1,'function'),
		//array('update_time','time',2,'function'),
		//array('update_time','time',1,'function'),
    );
	 
	public function _list($view,$map,$sort='sort',$pagesize=20){
		//计算总页数
		$count=$this->count($map);
		//实例化分页类
		import ( 'ORG.Util.Page' );
		$p=new Page($count,$pagesize);
		$list = $this->where($map)->alias('n')->join("left join __CATEGORY__ AS c on c.id=n.category_id")->Field("n.*,c.name as category_name")->order($sort)->limit($p->firstRow.', 
'.$p->listRows)->select();
        $pages=$p->show('news');
		$view->assign('list',$list);
		$view->assign('pages',$pages);
	}
	
	public function _listSelf($view,$map,$sort='sort',$pagesize=20){
		//计算总页数
		$count=$this->count($map);
		//实例化分页类
		import ( 'ORG.Util.Page' );
		$p=new Page($count,$pagesize);
		$list = $this->where($map)->alias('n')->join("left join __CATEGORY__ AS c on c.id=n.category_id")->Field("n.*,c.name as category_name")->order($sort)->limit($p->firstRow.',
'.$p->listRows)->select();
        $pages=$p->show('news');
		$view->assign('listSelf',$list);
		$view->assign('pagesSelf',$pages);
	}
	private function count($map){
		$re = $this->alias('n')->where($map)->Field('count(*) as tp_count')->find();
		return $re['tp_count'];
	}
	
	public function __list($_name='n',$_cate='c',$_module='m',$map=NULL,$order='sort',$count=20){
		$data=$this->alias($_name)->join("left join __CATEGORY__ {$_cate} on {$_cate}.id={$_name}.category_id")->join("left join __MODULE__ {$_module} on {$_module}.id={$_cate}.module_id")->Field($_name.".*,{$_cate}.title as category_title,{$_cate}.name as category_name,{$_module}.title as module_title,{$_module}.name as module_name")->where($map)->order($order)->limit("0,".$count)->select();
		return $data;
	}
	
	public function ___list($_name='n',$_cate='c',$_module='m',$map=NULL,$order='sort',$pagesize=20){
		$count1=$this->alias($_name)->join("left join __CATEGORY__ {$_cate} on {$_cate}.id={$_name}.category_id")->join("left join __MODULE__ {$_module} on {$_module}.id={$_cate}.module_id")->Field($_name.".*,{$_cate}.title as category_title,{$_cate}.name as category_name,{$_module}.title as module_title,{$_module}.name as module_name")->where($map)->order($order)->Field("count(".$_name.".id) as tp_count")->find();
		$count = $count1['tp_count'];
		import ( 'ORG.Util.Page' );
		$p=new Page($count,$pagesize);
		
		$list=$this->alias($_name)->join("left join __CATEGORY__ {$_cate} on {$_cate}.id={$_name}.category_id")->join("left join __MODULE__ {$_module} on {$_module}.id={$_cate}.module_id")->Field($_name.".*,{$_cate}.title as category_title,{$_cate}.name as category_name,{$_module}.title as module_title,{$_module}.name as module_name")->where($map)->order($order)->limit($p->firstRow.',
'.$p->listRows)->select();
		$pages=$p->show();
		$re['list']=$list;
		$re['pages']=$pages;
		return $re;
	}
	
	
	
}

?>